﻿// Decompiled with JetBrains decompiler
// Type: LayersParser.Objects.Command
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using LayersParser.Extensions;
using LayersParser.Interfaces;
using System.Collections.Generic;
using System.Xml.Linq;


namespace LayersParser.Objects;

public class Command : Common, ICommand
{
  private List<Param> _relatedParams = new List<Param>();
  private Layers _simulationLayer = new Layers();
  private Layers _downloadlayer = new Layers();
  private Layers _uilayer = new Layers();
  private Dialog dialog = new Dialog(true);
  private string _name = "";

  public virtual Dialog Dialog
  {
    get => this.dialog;
    set => this.dialog = value;
  }

  public Command()
  {
    this.Node = new XElement(new XElement((XName) nameof (Command), new object[3]
    {
      (object) new XAttribute((XName) nameof (Name), (object) ""),
      (object) new XElement((XName) "RoboticParamRef"),
      (object) new XElement((XName) "Layers", new object[3]
      {
        (object) new XElement((XName) nameof (UILayer)),
        (object) new XElement((XName) nameof (SimulationLayer)),
        (object) new XElement((XName) nameof (DownloadLayer))
      })
    }));
    this._uilayer.Node = this.Node.Element((XName) "Layers").Element((XName) nameof (UILayer));
    this._downloadlayer.Node = this.Node.Element((XName) "Layers").Element((XName) nameof (DownloadLayer));
    this._simulationLayer.Node = this.Node.Element((XName) "Layers").Element((XName) nameof (SimulationLayer));
  }

  public Command(XElement commandNode, List<Param> parameters)
  {
    this.Node = commandNode;
    this._name = (string) commandNode?.Attribute((XName) nameof (Name));
    if (commandNode?.Element((XName) "RoboticParamRef")?.Elements((XName) "Param") != null)
    {
      foreach (XElement element in commandNode.Element((XName) "RoboticParamRef").Elements((XName) "Param"))
      {
        if (element.Value != null && element.Value != "")
        {
          Param obj = parameters.ReturningParameter(element.Value);
          obj.Command = true;
          this._relatedParams.Add(obj);
        }
        else if (element.Attribute((XName) nameof (Name)) != null)
        {
          Param obj = parameters.ReturningParameter((string) element.Attribute((XName) nameof (Name)));
          obj.Command = true;
          this._relatedParams.Add(obj);
        }
      }
    }
    this.dialog = new Dialog(this.Node.Document.GoToOlpDialogs(this._name));
    this._uilayer = new Layers(this.Node.Element((XName) "Layers").Element((XName) nameof (UILayer)));
    this._downloadlayer = new Layers(this.Node.Element((XName) "Layers").Element((XName) nameof (DownloadLayer)));
    this._simulationLayer = new Layers(this.Node.Element((XName) "Layers").Element((XName) nameof (SimulationLayer)));
    this._uilayer.Params = this._downloadlayer.Params = this._simulationLayer.Params = this._relatedParams.ParToStr();
  }

  public override string Name
  {
    get => this._name;
    set
    {
      this._name = value;
      if (this.Node.Attribute((XName) nameof (Name)) != null)
        this.Node.Attribute((XName) nameof (Name)).Value = this._name;
      this.dialog.CommandName = this._name;
    }
  }

  public virtual List<Param> Params
  {
    get => this._relatedParams;
    set
    {
      this._relatedParams = value;
      this._uilayer.Params = this._simulationLayer.Params = this._downloadlayer.Params = this._relatedParams.ParToStr();
      XDocument document = this.Node.Document;
      if (document != null)
      {
        XElement roboticParamRef = document.GoToRoboticParamRef(this._name);
        if (roboticParamRef != null)
          roboticParamRef.Elements((XName) "Param").Remove<XElement>();
      }
      foreach (Param relatedParam in this._relatedParams)
      {
        this.Node.Document.GoToRoboticParamRef(this._name).Add((object) new XElement((XName) "Param", (object) relatedParam.Name));
        if (relatedParam.Node.Parent == null)
          this.Node.Document.GoToRoboticParams().Add((object) relatedParam.Node);
      }
    }
  }

  public Layers UILayer
  {
    get => this._uilayer;
    set => this._uilayer = value;
  }

  public Layers DownloadLayer
  {
    get => this._downloadlayer;
    set => this._downloadlayer = value;
  }

  public Layers SimulationLayer
  {
    get => this._simulationLayer;
    set => this._simulationLayer = value;
  }
}
